import pandas as pd
import requests
import json
import URL
import time

df = pd.read_excel('./情感分析素材/小米.xlsx')


def lax(text, s):
    data = json.dumps({'text': text})
    cline = requests.post(URL.QG, params=URL.Params, headers=URL.Headers, data=data)
    re = cline.json()['items'][0]
    x = lambda a: round(a * 100, 2)
    data1 = {
        '名称': df['会员'][s],
        '情感积极概率': f"{x(re['positive_prob'])}%",
        '情感消极概率': f"{x(re['negative_prob'])}%",
        '分类信任度': f"{x(re['negative_prob'])}%"
    }
    return data1


list1 = []
s = 0
for i in df['评价内容']:
    list1.append(lax(i, s))
    s += 1
    time.sleep(0.5)
re = pd.DataFrame(list1)
re.to_excel('result.xlsx', index=False, engine='openpyxl')
print("写入完成")
